var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
//引入获得地址的js文件
var qqmapsdk;
const app=getApp()


Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:"",  // 上传的图片
    titles:"",
    miaoshu:"",
    index:6,
    radio:"",  // 服务类型
    list:[{title:"",text:""}],
    title:{},
  },
  addList: function(){
    let newArray={
      title:"",
      text:"",
    }
    this.setData({
      list:this.data.list.concat(newArray)
    })  
  },
  delList: function (e) {
    console.log(e)
    let that=this
    let index=e.target.dataset.index
    let arrayLength=that.data.list.length
    let newArray=[]
    if(arrayLength>1){
      for (let i=0;i<arrayLength;i++){
        if(i!==index){
          newArray.push(that.data.list[i])
        }
      }
      that.setData({
        list:newArray
      })
    }else{
      wx.showToast({
        icon: 'none',
        title: '必须设置一个收费项目',
      })
    }
  },
  
  setInputValue: function(e) {
    console.log(e)
    let index = e.target.dataset.index //数组下标
    let tag = e.target.dataset.tag  //字段名称
    let array = this.data.list;
    array[index][tag] = e.detail.value  //赋值
    this.setData({
      addPrice: array
    })
  },
 
con:function(){
  console.log(this.data)
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  //获取标题
  getTitle(e){
    console.log(e.detail.value)
    this.setData({
      title:e.detail.value
    });
  },

   // 详情
   getXq(e){
    console.log(e.detail.value)
    this.setData({
      miaoshu:e.detail.value
    });
  },



  // 选着服务类型
  onChangeRadio(event) {
    console.log(event.detail)
    this.setData({
      radio: event.detail,
    });
  },


  // 获取当前时间
  //显示日期在页面上  yyy-MM-dd
  getTime(){
	  var now = new Date();
	  var year = now.getFullYear(); //得到年份
	  var month = now.getMonth();//得到月份
	  var date = now.getDate();//得到日期
	  var day = now.getDay();//得到周几
	  var hour = now.getHours();//得到小时
	  var minu = now.getMinutes();//得到分钟
	  var sec = now.getSeconds();//得到秒
	  month = month + 1;
	  if (month < 10) month = "0" + month;
	  if (date < 10) date = "0" + date;
	  if (hour < 10) hour = "0" + hour;
	  if (minu < 10) minu = "0" + minu;
	  if (sec < 10) sec = "0" + sec;
	  var time = "";
	  //精确到天
		time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
    // return time;
    //console.log(time)
    return time
},

// 点击上传图片
changeBigImg() {
    wx.showLoading({
      msg: '上传中...',
    })
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success :res=>{
        console.log(res)
        console.log(res.tempFiles[0].path)
        wx.uploadFile({
          url: "http://127.0.0.1:5000/upload",  //你的node服务端口
          filePath:res.tempFiles[0].path,//这里接受上传文件的path
          name:"file",
          header: {
            "content-type": "multipart/form-data"//注意
          },
          success :res=> {
            wx.hideLoading()
            wx.showToast({
              msg: '上传成功',
              icon: 'success',
              duration: 1000
            })
            console.log(JSON.parse(res.data).imgUrl)   // 上传图片后 返回的图片链接地址
            this.setData({
              img:JSON.parse(res.data).imgUrl
            })
          },
          fail:function(err){
            console.log(err);
          }
        })
      }
    });
   },




 // 提交
 addBtn(){
   const msg = this.data.list;
   console.log(msg)
  if(!this.data.img){
    wx.showModal({
      title: '提示',
      content:"请上传图书封面",
      showCancel:false,
      success:()=>{
        return
      }
    })
  }else if(!this.data.title){
    wx.showModal({
      title: '提示',
      content:"请输入服务标题！",
      showCancel:false,
      success:()=>{
        return
      }
    })
  }else if(!this.data.miaoshu){
    wx.showModal({
      title: '提示',
      content:"请输入图书详情内容！",
      showCancel:false,
      success:()=>{
        return
      }
    })
  }else if(!this.data.radio){
    wx.showModal({
      title: '提示',
      content:"请选图书类型！",
      showCancel:false,
      success:()=>{
        return
      }
    })
  }else{
    wx.showLoading({
      title: '请稍后...',
    })
    wx.request({ 
      url: 'http://127.0.0.1:5000/addBook', 
      method:"POST",
      data: {
        img:this.data.img,
        msg:JSON.stringify(msg),
        title:this.data.title,
        miaoshu:this.data.miaoshu,
        num:this.data.radio,
        shijian:this.getTime()
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:(res)=> {
        console.log(res.data)
        wx.hideLoading()
        if(res.data.code==200){
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: "发布成功！",
            showCancel:false,
            success:()=> {
              // wx.navigateBack({
              //   delta:1
              // })
              this.setData({
                img:"",  // 上传的图片
                title:"",
                miaoshu:"",
                radio:"",  // 服务类型
                list:this.data.list
              
              });
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: "发布失败！",
            showCancel:false,
            success () {
              
            }
          })
        }
      }
    })
  }
 },




})